
import { GroupInfo } from "../class/group";
import { ClusterIndependent } from "../class/group/clusters/room/independent";
// import { ClusterSingleRoom } from "../class/group/clusters/single";
import { ExtRoom } from "../extention/room";
import { errLog } from "../util/log";

const supportGroupTypes=[
    ClusterIndependent,
];

export const globalGroupManager={

    getGroups:function(){
        let groups=Game.groups;
        if(groups!=undefined && typeof groups === "object"){
            for(let _ in groups){// test if groups is empty
                return groups;
            }
        }
        // groups=this.resetGroups();
        this.restoreMemory();

        return Game.groups;
    },
    resetGroups:function(){
        let groups = {};
        for(let roomName in Game.rooms){
            let room = Game.rooms[roomName];
            if(!room.controller || !room.controller.my){
                continue;
            }
            groups[roomName] = new ClusterIndependent(room);
        }
        console.log("resetting groups\n\t - resetGroups()");
        Game.groups = groups;
        return groups;
    },
    run:function(){
        let groups = this.getGroups();

        const totalInfo = Game.groupInfo = new GroupInfo();
        
        for(const name in groups){
            const group = Game.groups[name];
            totalInfo.add(group.info);
        }

        if(Game.time%3 === 2 && Game.cpu.bucket>10){
            for(const name in groups){
                const group = Game.groups[name];
                if(!ExtRoom.get(group.spawnRoom).canSpawn()){
                    continue;
                }
                try{
                    group.spawn();
                }catch(e){
                    errLog(e);
                }
            }
        }

        for(let roomName in groups){
            let group = Game.groups[roomName];
            try{
                group.run();
            }catch(e){
                errLog(e);
            }
        }
        return OK;
    },
    saveMemory(){
        let objGroups = Game.groups;
        let memGroups = {};
        
        for(let mainRoomName in objGroups){
            let objGroup = objGroups[mainRoomName];
            let memGroup;
            try{
                memGroup = {
                    class: objGroup.getClassName(),
                    obj: objGroup.serialize(),
                };
            }catch(e){
                errLog(e);
            }

            memGroups[mainRoomName] = memGroup;
        }
        Memory.groups=memGroups;
        return OK;
    },
    getDefaultGroup(spawnRoom){
        console.log("colony group of "+spawnRoom.name+" has been reset to default");
        return new ClusterIndependent(spawnRoom);
    },
    restoreMemory(){//new
        let newGroups={};
        let memGroups = Memory.groups;
        if(typeof memGroups !== "object"){
            Memory.groups = memGroups = {};
        }
        for(let roomName in Game.rooms){
            let room = Game.rooms[roomName];
            if(!room || !room.controller || !room.controller.my){
                continue;
            }
            let memGroup = Memory.groups[roomName];
            let objGroup = undefined;
            if(memGroup === undefined){
                objGroup = this.getDefaultGroup(room);
            }else{
                for(let GroupType of supportGroupTypes){
                    if(GroupType.name === memGroup.class){
                        try{
                            objGroup = new GroupType().deserialize(memGroup.obj);
                            break;
                        }catch(e){
                            errLog(e);
                        }
                        try{
                            objGroup = new GroupType(room);
                            break;
                        }catch(e){
                            errLog(e);
                        }
                        objGroup = this.getDefaultGroup(room);
                        break;
                    }
                }
                if(objGroup === undefined){
                    objGroup = this.getDefaultGroup(room);
                }
            }
            newGroups[roomName] = objGroup;
        }
        Game.groups=newGroups;
        return OK;
    },
}